package Controller.Sign;


import Dao.Sign.UploaderDao;
import Model.SignModel.Sign;
import Model.SignModel.Uploader;
import Model.User;
import Service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import utils.permission.ConstantsUtils;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/Uploader")
public class UploaderController {
    @Autowired
    private UploaderDao uploaderDao;

    @Autowired
    private UserService userService;

    @RequestMapping("/findAll")
    @ResponseBody
    public PageInfo findAll(@RequestParam(value = "pn", defaultValue = "1") Integer pn, @RequestParam(value = "limit", defaultValue = "10") Integer limit, HttpServletRequest request,Uploader uploader) throws ParseException {
        User user = (User) request.getSession().getAttribute(ConstantsUtils.SessionObj.CURRENT_USER);
        String Role_id=this.userService.findRoleIDbyUserNumber(user.getUser_number());
        if(Role_id.equals("2")){
            String settleCode =userService.findSettleCodebyUserNumber(user.getUser_number());
            System.out.println("找到机构代码："+settleCode);
            uploader.setOrganization_id(settleCode);
            PageHelper.startPage(pn, limit);
            List<Uploader> list = uploaderDao.findAll(uploader);
            System.out.println("[获取到上传数据：]"+list);
            PageInfo page = new PageInfo(list);
            return page;
        }
        PageHelper.startPage(pn, limit);
        List<Uploader> list = uploaderDao.findAll(uploader);
        System.out.println("[获取到上传数据：]"+list);
        PageInfo page = new PageInfo(list);
        return page;
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public int deleteById(Integer id){
        int status = -1;
        System.out.println("[根据Id删除上传记录！]");
        try {
            uploaderDao.deleteById(id);
            System.out.println("删除成功！");
            status = 0;
        }catch (Exception e){
            e.printStackTrace();
        }
        return status;
    }

    @RequestMapping("/addUploader")
    @ResponseBody
    public int addUploader(Uploader uploader){
        int status = -1;
        System.out.println("[新增上传记录！]");
        try {
            uploaderDao.addUploader(uploader);
            System.out.println("上传成功！");
            status = 0;
        }catch (Exception e){
            e.printStackTrace();
        }
        return status;
    }
}
